<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <fsButton ref="button" plain size="default" :buttons="buttons" @clickHandler="buttonHandler"></fsButton>
		  <el-date-picker style="margin-left: 5px;" v-model="depreciationMonth" @change="searchHandler" format="YYYY-MM" value-format="YYYYMM" type="month" placeholder="请选择折旧月份"/>
      </div>
      <div class="right-panel">
        <ex-search ref="search" :searches="searches" @searchOk="searchHandler" :initParams="{depreciationMonth:this.depreciationMonth}" default-code="queryContent" placeholder="请输入资产编码"></ex-search>
      </div>
    </el-header>
    <el-main  class="nopadding">
		<ex-table id="depreciationLogTable" ref="table" :columns="columns" :url="this.$API.system.depreciationLog.list.url" :refs="this.$refs" search="search" @columnClick="columnClickHandler"></ex-table>
    </el-main>
	  <data-card ref="dataCard"></data-card>
  </el-container>
</template>

<script>
import ExTable from "@/components/exTable/index.vue";
import ExSearch from "@/components/exSearch/index.vue";
import BaseTemplate from "@/utils/baseTemplate";
import moment from "moment/moment";
import DataCard from "@/views/asset/card/card.vue";

export default {
  name: '接口任务',
  components: {
	  DataCard,
    ExSearch,
    ExTable,
  },
  data() {
    return {
      buttons: [
        {name: this.$t('button.export'), type: "info", icon: "el-icon-download", click: "export"},
		  // {name: this.$t('button.delete'), icon: "el-icon-delete", click: "del", type: "info", permission: ""},
      ],
      searches: [
        { type: "select", name: "折旧方式", code: "depreciationMethodList", placeholder: "请选择折旧方式", dictCode:'DEPRECIATION_METHOD',multiple:true},
        { type: "input", name: "资产编码", code: "assetCode", placeholder: "请输入资产编码"},
        { type: "daterange", name: "创建时间", code: "createTimeStart,createTimeEnd",startPlaceholder:"请输入开始日期", endPlaceholder: "请输入结束日期", format: "YYYY-MM-DD",valueFormat:"YYYYMMDD"},

      ],
      columns: [
		  {code: "assetCode", name: "资产编码", width: "200",click:"show1" },
		  {code: "depreciationDate", name: "折旧日期", width: "175", formatter: "DATE"},
		  {code: "depreciateValuePerMonth", name: "当月折旧额", width: "200"},
		  {code: "netValue", name: "本次净值", width: "200"},
		  {code: "lastNetValue", name: "上一次净值", width: "200"},
		  {code: "originalValue", name: "资产原值", width: "200"},
		  {code: "depreciatedMonths", name: "累计折旧月数", width: "180"},
		  {code: "depreciatedValues", name: "累计折旧额", width: "200"},
		  {code: "depreciationMethod", name: "折旧方式", width: "",formatter:"DICT",key: "DEPRECIATION_METHOD"},
		  {code: "createTime", name: "操作时间", width: "175", formatter: "DATETIME"},
      ],
      parentLevelCode: null,
		depreciationMonth: moment(new Date()).format("YYYYMM"),
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    buttonHandler(index, name) {
      this.$options.methods[name](this, index);
    },
    async searchHandler() {
      	let queryParams = {pathCode: this.parentLevelCode}
		queryParams.depreciationMonth = this.depreciationMonth;
      	await this.$refs.table.refresh(queryParams);
    },
    columnClickHandler(name,data){
      this.$options.methods[name](this, data);
    },
	  //批量删除
	  del(_this, index) {
		  let selections = _this.$refs.table.selections();
		  if (selections.length === 0) {
			  _this.$message.warning(_this.$t("common.please_select_data"));
			  return;
		  }
		  _this.$confirm(`确定删除选中的 ${selections.length} 项及其下面的接口任务日志吗？`, _this.$t('common.warn'), {
			  type: 'warning'
		  }).then(async () => {
			  _this.$refs.button.loading(index, true);
			  var delarray = [];
			  for (let index = 0; index < selections.length; index++) {
				  const element = selections[index];
				  delarray.push(element.id);
			  }
			  var res = await _this.$API.system.depreciationLog.deleteByIds.post(delarray);
			  if (res.code === 2000) {
				  _this.$message.success(_this.$t("code."+res.code));
				  _this.searchHandler();
				  _this.$store.dispatch("commit",selections[0].entityName);
			  }
			  _this.$refs.button.loading(index, false);
		  }).catch(() => {
			  _this.$refs.button.loading(index, false);
		  });
	  },
	  //查看
	  async show1(_this, row,index) {
		  _this.$refs.button.loading(index, true);
		  let assetCategoryData = await _this.$cache.data.loadAssetCategory(row.assetCategoryId);
		  try {
			  let entity = {cardId: assetCategoryData.assetCardId, _editFlag: false}
			  _this.$nextTick(async () => {
				  let assetRsp = await _this.$API.asset.asset.findById.post({id:row.assetId});
				  if(assetRsp.code === 2000 && assetRsp.data){
					  _this.$refs.button.loading(index, false);
					  _this.$refs.dataCard.open(Object.assign(entity, assetRsp.data));
				  }else{
					  _this.$refs.button.loading(index, false);
					  _this.$message.message("未查询到资产信息");
				  }
			  });
		  } catch {
			  _this.$refs.button.loading(index, false);
		  }
	  },
    //导出
    async export(_this,index) {
      let attachParams = {};
		attachParams.depreciationMonth = this.depreciationMonth;
      let exportApi = _this.$API.system.depreciationLog.export;
      let exTableRef = _this.$refs.table;
      let exportBtnRef = _this.$refs.button;
      let exportBtnIndex = index;
      await BaseTemplate.export(_this, exportApi, attachParams, exportBtnRef, exportBtnIndex, exTableRef);
    },
  }
}
</script>
